package com.dex4d.core;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dex4d.R;
import com.gyf.barlibrary.ImmersionBar;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by chengyuchun on 2018/10/17.
 */
public abstract class AppBaseFragment extends Fragment {
    protected Context mContext;
    protected View contentView;
    protected ImmersionBar mImmersionBar;
    protected Unbinder mUnbinder;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        contentView = inflater.inflate(getLayoutId(), container, false);
        mUnbinder = ButterKnife.bind(this,contentView);//绑定View
        initVariables();
        loadData();
        return contentView;
    }
    /**
     * 初始化沉浸式
     */
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.keyboardEnable(true).navigationBarWithKitkatEnable(false).init();
    }

    protected abstract int getLayoutId();

    protected abstract void initVariables();

    protected abstract void loadData();

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden && mImmersionBar != null)
            mImmersionBar.fullScreen(false).navigationBarColor(R.color.black);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null)
            mImmersionBar.destroy();
        if (mUnbinder != null)
            mUnbinder.unbind();//解绑View
    }
}
